<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 17/8/5
 * Time: 下午4:37
 */
namespace app\api\controller\v1;

use app\api\controller\Common;
use think\Controller;
// use app\common\lib\exception\ApiException;
// use app\common\lib\Aes;

class Index extends Common {

    /**
     * 栏目接口
     */
    public function index() {
       $heads = model('News')->getIndexHeadNormalNews();
       $heads = $this->getDealNews($heads);
       $position = model('News')->getPositionNormalNews();
       $positionon = $this->getDealNews($position);

       $result = [
            'heads'=>$heads,
            'position'=>$position,
       ];
       return show(config('code.success'), 'OK', $result);
    }

    /**
     * 客户端初始化接口
     * 1.检测app_s是否要升级
     */
    public function init(){
        //app_type 去ent_version查询
        try {
          $version = model("Version")->getLastNormalVersionByAppType($this->headers['app-types']);
        } catch (\Exception $e) {
            return new ApiException('error', 400);
        }

        if (empty($version)){
            return new ApiException('error', 400);
        }

        if ($version->version >$this->headers['version']) {
            $version->isUpdate = $version->is_force == 1? 2:1;
        }else{
            $version->isUpdate=0; //0 不更新  1 更新   2强制更新
        }

        //记录用户的基本信息   用于统计
        $actives = [
              'version' => $this->headers['version'],
              'app_type' => $this->headers['app-types'],
              'did' => $this->headers['did'],
              'version_code' => $this->headers['version'],
              'model' => $this->headers['model'],
        ];
        try {
            model('AppActive')->add($actives);
        } catch (\Exception $e) {
          // todo
          // Log::write();记录日志
        }

        return show(config('code.success'),'ok',$version,200);
    }


}